<?php
/*
	仓库管理
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//声明页面上方tab按钮
$tabs=array(
	array('op'=>'list','title'=>'仓库列表'),
	array('op'=>'edit','title'=>'创建仓库'),
);
set_html('tabs',$tabs);
//声明列表页
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('depot',$parameter,$total);
	foreach($list as &$item){
		$item['pcd']=json_decode($item['pcd'],true);
		$item['pcdtitle']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'];
	}
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'export'=>'export',//声明导出动作op，如无需导出功能，可不声明该字段
		'list'=>array('data'=>'list','tip'=>'供货商，打印商品标签，会根据订单的地址区域，来指定某个仓库','list'=>array(
			//列表展示列
			array('type'=>'string','name'=>'title','title'=>'仓库名称',),
			array('type'=>'string','name'=>'sn','title'=>'仓库编号',),
			array('type'=>'string','name'=>'pcdtitle','title'=>'仓库区域',),
			array('type'=>'string','name'=>'name','title'=>'联系人',),
			array('type'=>'string','name'=>'mobile','title'=>'联系电话',),
			array('type'=>'string','name'=>'address','title'=>'仓库地址',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'仓库名称','width'=>'15'),
		),
	);
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
//声明编辑页
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		//处理表单提交
		$post=get_postdata();
		$post['province']=$post['pcd']['province'];
		$post['city']=$post['pcd']['city'];
		$post['district']=$post['pcd']['district'];
		$post['street']=$post['pcd']['street'];
		//获取表单数据
		$data=array_fields(array('title','address','pcd','name','mobile','sn','province','city','district','street'),$post);
		//将数组类型表单数据转为字符串以备入库
		$data['pcd']=$data['pcd']?ijson_encode($data['pcd']):'';
		$id=intval($post['id']);
		if(!$id){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$this->pdo_insert('depot',$data);
		}else{
			$this->pdo_update('depot',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('修改成功！','','list');
	}
	//整理编辑页
	$id=intval($_RQ['id']);
	if(!$id){
		$item=array();
	}else{
		$item=$this->pdo_get('depot',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'门店信息设置',
		'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'仓库名称','must'=>true),
				array('type'=>'string','name'=>'sn','title'=>'仓库编号',),
				array('type'=>'string','name'=>'name','title'=>'联系人',),
				array('type'=>'string','name'=>'mobile','title'=>'联系电话',),
				array('type'=>'pcd','name'=>'pcd','title'=>'行政区划地址','help'=>'前端供货商，商品标签打印，会根据订单的区域，对应的仓库编号，打印出来'),
				array('type'=>'string','name'=>'address','title'=>'详细地址','help'=>''),
		),
	);
	$return['htmls']=$htmls;
	//返回前端
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_supplier_depot',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_supplier_depot',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
//导出动作
if($_RQ['op']=='export'){
	//整理筛选条件
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('depot',$parameter);
	if($list){
		foreach($list as &$item){
			//整理从数据库获取到的数据
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'仓库名称',
		'address'=>'仓库地址',
		'createtime'=>'创建时间',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'仓库列表');
}